//
//  MainTabBarController.m
//  TheRoadAD
//
//  Created by baixue on 16/8/4.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "MainTabBarController.h"

@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customTabBar];
    // Do any additional setup after loading the view.
}

-(void)customTabBar
{
    //把系统的tabBar 隐藏
    self.tabBar.hidden = YES;
    
    //创建一个视图去代替tabBar
    UIImageView *customBar = [[UIImageView alloc]init];
    //获取屏幕的尺寸
    CGSize size = [UIScreen mainScreen].bounds.size;
    
    //设置customBar位置和大小，屏幕的最低部
    customBar.frame = CGRectMake(0, size.height - 60, size.width, 60);
    customBar.image = [UIImage imageNamed:@"tabbg"];
    [self.view addSubview:customBar];
    
    
    for (int i = 0; i < 4; i ++) {
        
        //用Button去代替TabBarItem
        UIButton *button = [[UIButton alloc]init];
        
        button.frame = CGRectMake(i*size.width/4, 0, size.width/4, 60);
        
        //设置正常状态下得图片
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%d",i]] forState:UIControlStateNormal];
        //高亮状态下得图片
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%d",i]] forState:UIControlStateHighlighted];
        //设置选中状态下得图片
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_c%d",i]] forState:UIControlStateSelected];
        //设置标题
        [button setTitle:[NSString stringWithFormat:@"第%d页",i+1] forState:UIControlStateNormal];
        //设置标题颜色
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        //设置标题的字体
        button.titleLabel.font = [UIFont systemFontOfSize:10];
        
        // 设置标题的偏移量,四个参数分别对应上、左、下、右
        button.titleEdgeInsets = UIEdgeInsetsMake(10, -50, -5, 0);
        
        //设置图片的偏移量，四个参数分别对应上、左、下、右
        button.imageEdgeInsets = UIEdgeInsetsMake(-10, 0, 0, 0);
        
        //设置标记
        button.tag = 100 + i;
        [customBar addSubview:button];
    }
    
    //设置响应用户事件，UIImageView和UILabel默认是NO
    customBar.userInteractionEnabled = YES;
    
}



-(void)buttonClicked:(UIButton *)button
{
    //拿到所有的button
    for(int i = 0; i < 4; i++)
    {
        //从self.view的孙子视图中找
        UIButton *btn = (id)[self.view viewWithTag:100 + i];
        if (btn != button) {
            //设置为非选中状态
            btn.selected = NO;
        }
        
    }
    //点击哪一个按钮，选中哪一个
    button.selected = YES;
    //切换到相应的界面
    self.selectedIndex = button.tag - 100;
}


@end
